const service = require('../request').service;
const model = require("../../utils/modelUtil");

/**
 * 委托
 */
class Entrust {
    constructor(user) {
        this.user = user;
    }

    /**
     * 获取委托任务列表
     * @returns {Promise<unknown>}
     */
    getEntrustTaskList() {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/getCircleTaskList',
                data: model.userModel('getCircleTaskList', {}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 刷新委托任务
     * @param idArray 委托列表索引数组
     * @param type 刷新方式 1=银两 2=高级委托令
     * @returns {Promise<unknown>}
     */
    refreshEntrustTask(idArray, type) {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/changeDelegateTask',
                data: model.userModel('changeDelegateTask', {tIDArr: idArray, type: type}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 委托任务
     * @param taskID 委托列表索引
     * @returns {Promise<unknown>} bool
     */
    entrustTask(taskID) {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/delegateTask',
                data: model.userModel('delegateTask', {taskID: taskID}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200&&resData.MsgData.error===0&&resData.MsgData[resData.MsgData.p].eRet===1);
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 领取委托奖励
     * @param taskID 委托列表索引
     * @returns {Promise<unknown>}
     */
    getEntrustReward(taskID) {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/circleTaskAward',
                data: model.userModel('circleTaskAward', {taskID: taskID}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }
}

module.exports = Entrust;